package com.tianzeculture.stagebao.activity.login;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.application.StageBaoApplication;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.net.UserRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.Util;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/24 0024.
 * 重置密码
 */

public class ForgetPwdActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;

    @BindView(R.id.edtPhone)
    EditText edtPhone;
    @BindView(R.id.tvGetCode)
    TextView tvGetCode;
    @BindView(R.id.edtCode)
    EditText edtCode;
    @BindView(R.id.edtPwd)
    EditText edtPwd;
    @BindView(R.id.edtPwdAgain)
    EditText edtPwdAgain;
    @BindView(R.id.btnReset)
    Button btnReset;

    @Request
    UserRestUsage userRestUsage = null;

    private int GET_CODE = 101;//获取验证码
    private int RESET_PWD = 102;
    private String code = "";//验证码
    TimeCount time;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_forgetpwd;
    }

    @Override
    public void initUI() {
        super.initUI();
        StageBaoApplication.addActivity(this);

        tvTitle.setText(getString(R.string.reset_pwd));

        time = new TimeCount(10000, 1000);
    }

    @OnClick(R.id.tvGetCode)
    public void getCode(View view) {
        if (Util.isPhone(edtPhone.getText().toString().trim())) {//1注册 2重置密码
            userRestUsage.getCode(GET_CODE, edtPhone.getText().toString().trim(), 2);
            time.start();
        }
    }

    @OnClick(R.id.btnReset)
    public void resetPwd() {
        if ("".equals(edtPhone.getText().toString().trim())) {
            ToastUtil.showShort(this, "手机号码不能为空!");
        } else if ("".equals(edtCode.getText().toString().trim())) {
            ToastUtil.showShort(this, "验证码不能为空!");
        } else if ("".equals(edtPwd.getText().toString().trim())) {
            ToastUtil.showShort(this, "密码不能为空!");
        } else if ("".equals(edtPwdAgain.getText().toString().trim())) {
            ToastUtil.showShort(this, "再次密码不能为空!");
        } else if (!edtPwd.getText().toString().trim().equals(edtPwdAgain.getText().toString().trim())) {
            ToastUtil.showShort(this, "两次密码不一致!");
        } else if (!edtCode.getText().toString().trim().equals(code)) {
            ToastUtil.showShort(this, "验证码错误!");
        } else {
            showProgressDialog();
            userRestUsage.resetPassword(RESET_PWD, edtPhone.getText().toString().trim(),
                    edtCode.getText().toString().trim(), edtPwd.getText().toString().trim());
        }
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == GET_CODE) {
            if (msg.getObj() != null) {
                ResultBean resultBean = (ResultBean) msg.getObj();
//                ToastUtil.show(this,code);
                code = resultBean.getCode();
            }
        } else if (taskId == RESET_PWD) {
            dismissProgressDialog();
        }
    }


    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tvGetCode.setClickable(false);
            tvGetCode.setText(millisUntilFinished / 1000 + "s");
        }

        @Override
        public void onFinish() {
            tvGetCode.setText("重新获取验证码");
            tvGetCode.setClickable(true);
        }
    }


}
